package com.firmail.service.impl;

import com.firmail.dao.MockApiInfoMapper;
import com.firmail.entity.MockApiInfo;
import com.firmail.entity.MockApiInfoExample;
import com.firmail.entity.vo.MockInfoVO;
import com.firmail.enumeration.impl.MajorTypeEnum;
import com.firmail.enumeration.impl.MockTypeEnum;
import com.firmail.func.Converter;
import com.firmail.model.Pager;
import com.firmail.repository.MockApiInfoRepository;
import com.firmail.service.IMockApiInfoService;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(/*value = "transactionManager",*/rollbackFor = Exception.class)
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MockApiInfoServiceImpl implements IMockApiInfoService<MockApiInfo, MockInfoVO> {
    @Deprecated
    @Autowired
    MockApiInfoRepository mockApiInfoRepository;

    @Autowired
    MockApiInfoMapper mapper;

    @Override
    public int saveOrUpdateMockData(MockInfoVO mockInfoVO) {

        MockApiInfo searchModel= new MockApiInfo();
        BeanUtils.copyProperties(mockInfoVO, searchModel);
        searchModel.setCreatetime(mockInfoVO.getCreatetime());

        MockApiInfoExample example = new MockApiInfoExample();
        MockApiInfoExample.Criteria criteria;
        //精确匹配
        if (searchModel != null) {
            criteria = example.createCriteria();

            //reqAddr
            if (!StringUtils.isBlank(searchModel.getReqAddr()))
                criteria.andReqAddrEqualTo(StringUtils.trim(searchModel.getReqAddr()));
            //reqMethod
            if (StringUtils.isNotBlank(searchModel.getReqMethod()))
                criteria.andReqMethodEqualTo(StringUtils.trim(searchModel.getReqMethod()));
            //dataType
            if (StringUtils.isNotBlank(searchModel.getDataType()))
                criteria.andDataTypeEqualTo(StringUtils.trim(searchModel.getDataType()));
        }

        List<MockApiInfo> mockApiInfos = mapper.selectByExample(example);

        int val;
        if (mockApiInfos == null || mockApiInfos.size() == 0) {
            val = mapper.insertSelective(searchModel);
        } else {
            val = mapper.updateByExampleSelective(searchModel, example);
        }
        return val;
    }

    @Override
    public List<MockInfoVO> getAPIDataByOrder(MockInfoVO searchModel, int pageNum, int pageSize) {
        MockApiInfoExample example = new MockApiInfoExample();
        example.setOrderByClause("createtime desc");
        PageHelper.startPage(pageNum, pageSize);
        List<MockApiInfo> apiInfos = mapper.selectByExample(example);

        List<MockInfoVO>  mockInfoVOList= new ArrayList<>();
        MockInfoVO mockInfoVO;
        for (MockApiInfo apiInfo:apiInfos){
            mockInfoVO=new MockInfoVO();
            BeanUtils.copyProperties(apiInfo,mockInfoVO);
            mockInfoVO.setCreatetime(apiInfo.getCreatetime());
            mockInfoVOList.add(mockInfoVO);
        }
        return mockInfoVOList;
    }

    @Override
    public int createApiData(MockInfoVO mVO) {

        MockApiInfo apiInfo= new MockApiInfo();
        BeanUtils.copyProperties(mVO,apiInfo);
        apiInfo.setCreatetime(mVO.getCreatetime());

        return mapper.insertSelective(apiInfo);
    }

    private List<MockApiInfo> getMultiPageListByModel(MockInfoVO searchModel) {
        MockApiInfoExample example = new MockApiInfoExample();
        //gender, stuName
        MockApiInfoExample.Criteria criteria;
        //name 接口名
        if (searchModel != null) {
            criteria = example.createCriteria();
            if (!StringUtils.isBlank(searchModel.getName())) {
                criteria.andNameLikeInsensitive("%" + searchModel.getName().trim() + "%");
            }
            //creator
            if (!StringUtils.isBlank(searchModel.getCreator())) {
                criteria.andCreatorLikeInsensitive("%" + StringUtils.trim(searchModel.getCreator()) + "%");
            }
            //major
            if (!StringUtils.isBlank(searchModel.getMajor()))
                criteria.andMajorEqualTo(StringUtils.trim(searchModel.getMajor()));
            //reqAddr
            if (!StringUtils.isBlank(searchModel.getReqAddr()))
                criteria.andReqAddrLikeInsensitive(StringUtils.trim(searchModel.getReqAddr()) + "%");
            //reqMethod
            if (!StringUtils.isBlank(searchModel.getReqMethod()))
                criteria.andReqMethodEqualTo(StringUtils.trim(searchModel.getReqMethod()));
            //dataType
            if (!StringUtils.isBlank(searchModel.getDataType()))
                criteria.andDataTypeEqualTo(StringUtils.trim(searchModel.getDataType()));
        }
        example.setOrderByClause("createtime desc");
        return mapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public Pager<MockApiInfo> getPageByModel(MockInfoVO searchModel, int pageNum, int pageSize) {

        Pager<MockApiInfo> result = new Pager<MockApiInfo>(pageNum, pageSize, getMultiPageListByModel(searchModel));

        return result;
        //return result;
    }

    public Pager<MockInfoVO> getPageVOByModel(MockInfoVO searchModel, int pageNum, int pageSize) {

        List<MockInfoVO> mockInfoVOList=new ArrayList<>();
        //final MockInfoVO mockInfoVO;
        this.getMultiPageListByModel(searchModel).forEach((mockApiInfo)->{
            MockInfoVO mockInfoVO= new MockInfoVO();
            BeanUtils.copyProperties(mockApiInfo, mockInfoVO);
            mockInfoVO.setCreatetime(mockApiInfo.getCreatetime());
            Converter<String,Integer> converter= Integer::valueOf;
            mockInfoVO.setReqMethod(MockTypeEnum.getName(converter.convert(mockApiInfo.getReqMethod())));
            mockInfoVO.setMajor(MajorTypeEnum.getName(mockApiInfo.getMajor()));
            mockInfoVOList.add(mockInfoVO);
        });

        return new Pager<>(pageNum, pageSize, mockInfoVOList);

    }

    @Deprecated
    public List<MockApiInfo> getAllAPIData() {

        List<MockApiInfo> apiInfos = mockApiInfoRepository.findAll();
        return apiInfos;

    }

    @Deprecated
    @Override
    public MockApiInfo createAPIData(MockInfoVO mVO) {
        MockApiInfo apiInfo= new MockApiInfo();
        BeanUtils.copyProperties(mVO,apiInfo);
        apiInfo.setCreatetime(mVO.getCreatetime());
        //return mapper.insertSelective(apiInfo);
        return mockApiInfoRepository.save(apiInfo);
    }
}
